package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.example.entity.Cart;
import org.example.service.CartService;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    // 1. 查询购物车信息
    @GetMapping("/listCart")
    public List<Cart> listCart(@RequestParam("userId") String userId,
            @RequestParam(value = "businessId", required = false) Integer businessId) {
        return cartService.listCart(userId, businessId);
    }

    // 2. 添加购物车信息
    @PostMapping("/saveCart")
    public int saveCart(@RequestParam("userId") String userId, @RequestParam("businessId") Integer businessId,
            @RequestParam("foodId") Integer foodId) {
        return cartService.saveCart(userId, businessId, foodId);
    }

    // 3. 更新购物车数量
    @PutMapping("/updateCart")
    public int updateCart(@RequestParam("userId") String userId, @RequestParam("businessId") Integer businessId,
            @RequestParam("foodId") Integer foodId, @RequestParam("quantity") Integer quantity) {
        return cartService.updateCart(userId, businessId, foodId, quantity);
    }

    // 4. 删除购物车信息
    @DeleteMapping("/removeCart")
    public int removeCart(@RequestParam("userId") String userId, @RequestParam("businessId") Integer businessId,
            @RequestParam(value = "foodId", required = false) Integer foodId) {
        return cartService.removeCart(userId, businessId, foodId);
    }
}